package com.zhenyi.liuxue.mini;

import java.util.List;

import com.jfinal.kit.Kv;
import com.jfinal.kit.LogKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.liuxue.common.model.LxActivity;
import com.liuxue.common.model.LxCertificate;
import com.liuxue.common.model.LxCompany;
import com.liuxue.common.model.LxPolicy;
import com.liuxue.common.model.LxRange;
import com.liuxue.common.model.LxUser;
import com.utils.AuthUtils;
import com.zhenyi.liuxue.index.LXController;

public class MiniController extends LXController {

	protected LxUser getUser() {
		String openId = getPara("openId");
		String uid = AuthUtils.getTokenUid(this);
		if (StrKit.notBlank(uid)) {
			LxUser user = LxUser.dao.findById(uid);
			if (user != null) {
				return user;
			}
		}
		if (StrKit.notBlank(openId)) {
			// return LxUser.dao.findById(Long.valueOf(uid));
			Kv kv = Kv.by("openId", openId);
			SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindUser", kv);
			return LxUser.dao.findFirst(sqlPara);
		} else {
			return null;
		}
	}

	protected LxUser getH5User() {
		return LxUser.dao.findById(Long.valueOf(AuthUtils.getTokenUid(this)));
	}

	/**
	 * 公司,城市,省份,任一在范围内,即通过
	 * 
	 * @param policyId
	 * @param activityId
	 * @param companyId
	 * @param city
	 * @param province
	 * @return
	 */
	private boolean isInRange(Long policyId, Long activityId, Long certId, Long companyId, String city,
			String province) {
		if (policyId != null && activityId != null) {
			LogKit.error("policyId,activityId not null");
			return false;
		}
		String name = "";
		Long value = null;
		if (policyId != null) {
			name = "policyId";
			value = policyId;
		}
		if (activityId != null) {
			name = "activityId";
			value = activityId;
		}

		if (certId != null) {
			name = "certId";
			value = certId;
		}

		if (companyId != null) {
			Kv kv = Kv.by(name, value);
			kv.set("companyId", companyId);
			SqlPara sqlPara = db.getSqlPara("LXFindItemRange", kv);
			List<Record> items = db.find(sqlPara);
			if (items.size() == 0) {
				// if (defaultValue != null) {
				// return defaultValue;
				// }
			}
			if (items.size() > 0) {
				return true;
			}
		}
		if (StrKit.notBlank(city)) {
			city = city.replace("市", "");
		}
		if (StrKit.notBlank(province)) {
			province = province.replace("省", "");
		}
		Kv kv = Kv.by(name, value);
		kv.set("province", province);
		SqlPara sqlPara = db.getSqlPara("LXFindItemRange", kv);
		List<LxRange> items = LxRange.dao.find(sqlPara);
		if (items.size() == 0) {
			// if (defaultValue != null) {
			// return defaultValue;
			// }
			return true;
		}
		for (LxRange lxRange : items) {
			String rangeCity = lxRange.getCity();
			String rangeProvince = lxRange.getProvince();
			if (StrKit.isBlank(rangeCity)) {
				if (rangeProvince.equals(province)) {
					return true;
				}
			} else {
				if (rangeProvince.equals(province) && rangeCity.equals(city)) {
					return true;
				}
			}
		}
		// if (StrKit.notBlank(city)) {
		// city = city.replace("市", "");
		// Kv kv = Kv.by(name, value);
		// kv.set("city", city);
		// SqlPara sqlPara = db.getSqlPara("LXFindItemRange", kv);
		// List<Record> items = db.find(sqlPara);
		// if (items.size() > 0) {
		// return true;
		// }
		// }
		// if (StrKit.notBlank(province)) {
		// province = province.replace("省", "");
		// Kv kv = Kv.by(name, value);
		// kv.set("province", province);
		// SqlPara sqlPara = db.getSqlPara("LXFindItemRange", kv);
		// List<Record> items = db.find(sqlPara);
		// if (items.size() > 0) {
		// return true;
		// }
		// }
		return false;
	}

	private final int TYPE_POLICY = 1;
	private final int TYPE_ACTIVITY = 2;
	private final int TYPE_CERTIFICATE = 3;

	/**
	 * 全部
	 */
	private final int RANGE_TYPE_ALL = 1;
	/**
	 * 签约大学
	 */
	private final int RANGE_TYPE_SIGN_SCHOOL = 1;
	/**
	 * 省市
	 */
	private final int RANGE_TYPE_LOCATIONS = 1;
	/**
	 * 二级代理
	 */
	private final int RANGE_TYPE_PROXY = 1;

	private boolean isItemInRange(Long id, int type) {
		Long schoolId;
		int rangeType;
		if (type == TYPE_POLICY) {
			LxPolicy policy = LxPolicy.dao.findById(id);
			schoolId = policy.getSchoolId();
			rangeType = policy.getRangeType();
		} else if (type == TYPE_ACTIVITY) {
			LxActivity activity = LxActivity.dao.findById(id);
			schoolId = activity.getSchoolId();
			rangeType = activity.getRangeType();
		} else if (type == TYPE_CERTIFICATE) {
			LxCertificate certificate = LxCertificate.dao.findById(id);
			schoolId = certificate.getSchoolId();
			rangeType = certificate.getRangeType();
		} else {
			return false;
		}
		if (rangeType == RANGE_TYPE_ALL) {
			return true;
		}
		LxUser user = getUser();
		LxCompany company = user.getCompany();
		if (rangeType == RANGE_TYPE_SIGN_SCHOOL) {
			if (company == null) {
				return false;
			}
			Long companyId = company.getId();
			Kv kv = Kv.by("schoolId", schoolId);
			kv.set("companyId", companyId);
			SqlPara sqlPara = db.getSqlPara("LXFindCompanySignSchool", kv);
			List<Record> items = db.find(sqlPara);
			if (items.size() == 0) {
				return false;
			}
			String city = user.getCity();
			String province = user.getProvince();
			if (type == TYPE_POLICY) {
				return isInRange(id, null, null, null, city, province);
			} else if (type == TYPE_ACTIVITY) {
				return isInRange(null, id, null, null, city, province);
			} else if (type == TYPE_CERTIFICATE) {
				return isInRange(null, null, id, null, city, province);
			}
		} else if (rangeType == RANGE_TYPE_LOCATIONS) {
			if (company == null) {
				return false;
			}
			Long companyId = company.getId();
			String city = user.getCity();
			String province = user.getProvince();
			if (type == TYPE_POLICY) {
				return isInRange(id, null, null, companyId, city, province);
			} else if (type == TYPE_ACTIVITY) {
				return isInRange(null, id, null, companyId, city, province);
			} else if (type == TYPE_CERTIFICATE) {
				return isInRange(null, null, id, companyId, city, province);
			}
		} else if (rangeType == RANGE_TYPE_PROXY) {
			// 二级代理
			// 一级代理公司
			Long pCompanyId = company.getCompanyId();
			// 当前公司
			Long companyId = company.getId();
			Kv kv = Kv.by("schoolId", schoolId);
			if (pCompanyId != null) {
				kv.set("companyId", pCompanyId);
			} else {
				kv.set("companyId", companyId);
			}
			SqlPara sqlPara = db.getSqlPara("LXFindCompanySignSchool", kv);
			List<Record> items = db.find(sqlPara);
			if (items.size() > 0) {
				return true;
			}
		}
		return false;

	}

	/**
	 * 政策是范围要求
	 * 
	 * @param policyId
	 * @return
	 */
	protected boolean isPolicyInRange(Long policyId) {
		return isItemInRange(policyId, TYPE_POLICY);
	}

	/**
	 * 活动是否在范围内
	 * 
	 * @param activityId
	 * @return
	 */
	protected boolean isActivityInRange(Long activityId) {
		return isItemInRange(activityId, TYPE_ACTIVITY);
	}

	/**
	 * 证书是否在范围内
	 * 
	 * @param certId
	 * @return
	 */
	protected boolean isCertificateInRange(Long certId) {
		return isItemInRange(certId, TYPE_CERTIFICATE);
	}

}
